// pages/mdhy/index.js
import {
  commissionVerifyList,
  commissionVerifyVerify,
  checkFromIdMatrix,
  getLiveCode
} from '../../utils/api'
import {
  formatTime
} from '../../utils/util'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: [{
      label: '全部',
      key: ''
    }, {
      label: '待核验',
      key: '10'
    }, {
      label: '已核验',
      key: '20'
    }],
    tabIndex: 0,
    status: '',
    lists: [],
    modalShow: false,
    id: '',
    disabled: false,
    code: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this._getData()
  },
  _getData() {
    commissionVerifyList(this.data.status).then(lists => {
      lists.map(item => {
        item.createtime = formatTime(new Date(item.createtime * 1000))
      })
      this.setData({
        lists
      })
    })
  },
  changeTab(e) {
    let data = e.currentTarget.dataset
    this.setData({
      tabIndex: data.index,
      status: data.key
    })
    this._getData()
  },
  verify(e) {
    let that = this
    const {status, id} = e.currentTarget.dataset
    if (status == 10) {
      wx.getSetting({
        success(res) {
          console.log(res)
          if (!res.authSetting['scope.userLocation']) {
            wx.authorize({
              scope: 'scope.userLocation',
              success() {
                wx.showToast({ //这里提示失败原因
                  title: '授权成功！',
                  duration: 1500
                })
              },
              fail() {
                // wx.showToast({
                //   title: '需要授权位置信息',
                //   icon: 'none'
                // })
              }
            })
          } else {
            wx.getLocation({
              isHighAccuracy: true,
              type: 'gcj02',
              success (res) {
                checkFromIdMatrix({id, from: res.latitude + ',' + res.longitude}).then(res => {
                  if (res.checkResult) {
                    that.setData({
                      modalShow: true,
                      id
                    })
                  } else {
                    wx.showToast({
                      title: '距离' + res.matrixValue + '公里',
                      icon: 'error'
                    })
                  }
                })
              }
             })
          }
        }
      })
      
    }
  },
  codeBlur(e) {
    this.setData({
      code: e.detail.value,
    })
  },
  cancel() {
    this.setData({
      modalShow: false,
      code: '',
      id: ''
    })
  },
  confirm() {
    const {
      id,
      code,
      disabled
    } = this.data
    if (id && code && !disabled) {
      commissionVerifyVerify({
        id,
        code
      }).then(() => {
        wx.showToast({
          title: '操作成功',
        })
        this._getData()
        this.cancel()
      })
    } else {
      wx.showToast({
        title: '请输入验证码',
        icon: 'none'
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})